#!/usr/bin/env python
#-*- coding: utf-8 -*-
import gtk
import pango

from IconFactory import IconFactory

class ButtonFactory(object):
	def __init__(self, iconFactory = None):
		self.defaultIcon = 'gnome-fs-regular'
		self.iconSize = gtk.ICON_SIZE_BUTTON
		self.toolTipWidget = None
		self.width = -1
		self.height = -1
		self.labelAttributes = []
		self.events = {}
		self.minLabelWidth = -1
		self.labelAlignment = 0.0
		self.iconFactory = IconFactory() if not iconFactory else iconFactory
	
	def addEvent(self, event, *callbackAndParameters):
		self.events[event] = callbackAndParameters
		return self
		
	def addLabelAttribute(self, labelAttribute):
		self.labelAttributes.append(labelAttribute)
		return self
		
	def clearLabelAttributes(self):
		self.labelAttributes = []
		return self
	
	def createLabel(self, labels):
		if not labels:
			labels = []
		elif isinstance(labels, str):
			labels = [labels]
			
		vBox = gtk.VBox()
		
		for index, text in enumerate(labels):
			if not text:
				continue
			label = gtk.Label(text)
			label.set_single_line_mode(True)
			
			if self.minLabelWidth == -1:
				label.set_width_chars(len(text))
			else:
				label.set_width_chars(self.minLabelWidth)
			label.set_ellipsize(pango.ELLIPSIZE_END)
			
			if len(self.labelAttributes) > index:
				attribute = self.labelAttributes[index]
				if attribute:
					itemStyle = pango.AttrList()
					itemStyle.insert(attribute)
					label.set_attributes(itemStyle)
			label.set_alignment(self.labelAlignment, 0.5)
			
			vBox.pack_start(label, True, True, 0)
		return vBox
	
	def createIcon(self, icon):
		if isinstance(icon, str) or isinstance(icon, unicode):
			if not icon:
				icon = self.defaultIcon
			iconPixbuf = self.iconFactory.iconForIconName(icon, self.iconSize)
		elif isinstance(icon, gtk.gdk.Pixbuf):
			iconPixbuf = icon
		else:
			return None
		if not iconPixbuf:
			iconPixbuf = self.iconFactory.iconForIconName(self.defaultIcon, self.iconSize)
			
		buttonIcon = gtk.Image()
		buttonIcon.set_from_pixbuf(iconPixbuf)
		return buttonIcon
			
	def create(self, labels = None, icon = None, toolTipText = ""):
		button = gtk.Button()
		
		button.set_relief(gtk.RELIEF_NONE)
		button.set_focus_on_click(False)
		
		for event, callback in self.events.iteritems():
			button.connect(event, *callback)
			
		hBox = gtk.HBox()
		
		buttonIcon = self.createIcon(icon)
		if buttonIcon: 
			hBox.pack_start(buttonIcon, False, False, 0)

		labelBox = self.createLabel(labels)
		if labelBox:
			hBox.pack_start(labelBox,True, True, 0)
		
		if self.toolTipWidget and toolTipText:
			self.toolTipWidget.set_tip(button, toolTipText, tip_private=None)
		
		button.add(hBox)
		button.show_all()
		return button

if __name__ == "__main__":
	window = gtk.Window(gtk.WINDOW_TOPLEVEL)
	window.connect('delete-event', gtk.main_quit)
	vbox = gtk.VBox()
	
	buttonFactory = ButtonFactory()
	buttonFactory.iconSize = 64
	button = buttonFactory.create("First", "www")
	vbox.pack_start(button)
	button = buttonFactory.create("Second", "font-x-generic")
	vbox.pack_start(button)
	
	window.add(vbox)
	window.show_all()
	gtk.main()

